package org.bilinc.gz001;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Utils {
    public static void showErrorWritingDialog(final JFrame frame) {
        JOptionPane.showMessageDialog(frame,
                ERROR_BODY,
                ERROR_TITLE,
                JOptionPane.ERROR_MESSAGE);
    }

    public static void showIncorrectData(Container frame, String field) {
        JOptionPane.showMessageDialog(frame,
                WARNING_BODY + field,
                WARNING_TITLE,
                JOptionPane.WARNING_MESSAGE);
    }

    public static final String WARNING_BODY = "Недопустимое значение поля ";
    public static final String ERROR_BODY = "Ошибка чтения или записи отчета";
    public static final String ERROR_TITLE = "Ошибка";
    public static final String WARNING_TITLE = "Внимание";

    public static final String[] WORKED_HEADER = new String[]{"Таб. №", "Город вахт", "ФИО", "Р-д", "Часов", "Смен", "Отпуск", "Разница", "ВЭМ"};
    public static final int DAYS_PER_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static final int[] WORKED_COLUMN_WIDTH = {50, 155, 100, 40, 40, 35, 45, 60, 45};
    public static final int MONTHS_COUNT = 12;
    public static final int W_JAN[] = {1, 2, 3, 4, 5, 6, 7, 8};
    public static final int W_MAR[] = {8};
    public static final int W_MAY[] = {1, 2, 3, 9, 10};
    public static final int W_JUN[] = {12};
    public static final int W_NOV[] = {4};

    public static final int JAN = 0;
    public static final int MAR = 2;
    public static final int MAY = 4;
    public static final int JUN = 5;
    public static final int NOV = 10;

    public class Year {
        private List<Month> mMonths;

        public Year(boolean isLeap) {
            for (int i = 0; i < MONTHS_COUNT; i++) {
                /*for feb 29 days per month if leap year*/
                mMonths.add(new Month((isLeap && i == 1) ? DAYS_PER_MONTH[i] + 1 : DAYS_PER_MONTH[i]));
            }
        }
    }

    public class Month {

        private List<Day> mDays;
        private int mDaysCount;

        public Month(int daysCount) {
            mDaysCount = daysCount;
            mDays = new ArrayList<Day>(daysCount);

            for (Day day : mDays) {
                day.mDayType = DayType.NOT_SET;
                day.mIsWeekend = false;
            }
        }
    }

    /**
     * Тип Дня
     */
    public static enum DayType {
        NOT_SET("Межвахтовый отдых"),
        STUDENT_VACATION("Учебный отпуск"),
        STUDENT_TRIP("Учебная командировка"),
        BUSINESS_TRIP("Командировка"),
        WORK_IN_DAY("Работа в лень"),
        WORK_IN_NIGHT("Работа в ночь"),
        WEEKEND("Выходной"),
        PRE_HOLIDAY("Предпраздничный рабочий день");

        private String type;

        public String getType() {
            return type;
        }

        DayType(String type) {
            this.type = type;
        }
    }

    public class Day {
        public boolean mIsWeekend;
        public DayType mDayType;
    }

    public static int getMonthByYearDay(final int day) {
        int days = 0;
        for (int i = 0; i < MONTHS_COUNT; i++) {
            days += DAYS_PER_MONTH[i];
            if (days > day) {
                return i;
            }
        }
        return -1;
    }

    public static int getMonthDayByYearDay(final int day) {
        int monthDay = day;
        for (int i = 0; i < MONTHS_COUNT; i++) {
            if ((monthDay - DAYS_PER_MONTH[i]) >= 0) {
                monthDay -= DAYS_PER_MONTH[i];
            } else {
                return monthDay + 1;
            }
        }
        return -1;
    }

    public static boolean isHoliday(int currentDay) {
        int month = getMonthByYearDay(currentDay);
        int day = getMonthDayByYearDay(currentDay);
        switch (month) {
            case Utils.JAN:
                for (int i = 0; i < W_JAN.length; i++) {
                    if (day == W_JAN[i]) {
                        return true;
                    }
                }
                break;
            case Utils.MAR:
                for (int i = 0; i < W_MAR.length; i++) {
                    if (day == W_MAR[i]) {
                        return true;
                    }
                }
                break;
            case Utils.MAY:
                for (int i = 0; i < W_MAY.length; i++) {
                    if (day == W_MAY[i]) {
                        return true;
                    }
                }
                break;
            case Utils.JUN:
                for (int i = 0; i < W_JUN.length; i++) {
                    if (day == W_JUN[i]) {
                        return true;
                    }
                }
                break;
            case Utils.NOV:
                for (int i = 0; i < W_NOV.length; i++) {
                    if (day == W_NOV[i]) {
                        return true;
                    }
                }
                break;
        }
        return false;
    }

    public static List<Integer> getDividersList(final List<List<String>> testData) {
        ArrayList<Integer> dividers = new ArrayList<Integer>();

        for (int i = 0; i < testData.size(); i++) {
            if (testData.get(i).size() == 2) {
                dividers.add(i);
            } else if (testData.get(i).get(0) != null && testData.get(i).get(2) == null) {
                dividers.add(i);
            }
        }
        return dividers;
    }

    public static List<Integer> getDividersList(final Object[][] testData) {
        ArrayList<Integer> dividers = new ArrayList<Integer>();

        for (int i = 0; i < testData.length; i++) {
            if (testData[i].length == 2) {
                dividers.add(i);
            }
        }
        System.out.println(dividers);
        return dividers;
    }

    @Deprecated
    public static Object[][]  convertListListToArrayArray(UiTableData list) {
        Object[][] array = new Object[list.getRowCount()][];
        for (int i = 0; i < list.getRowCount(); i++) {
            array[i] = list.getRow(i).toArray();
        }
        return array;
    }

    public static List<String> split(String monthData, String pattern) {
        List<String> days = new ArrayList<String>();
        int previousIndex = 0;
        int index = monthData.indexOf(pattern);
        while (index >= 0) {
            days.add(monthData.substring(previousIndex, index));
            previousIndex = index + 1;
            index = monthData.indexOf(pattern, previousIndex);
        }
        return days;
    }
}
